﻿using RabbitMQ.Client;
using System.Configuration;

namespace Dysoft.RabbitMQ
{
    /// <summary>
    /// 基于RabbitMQ的消息队列模块
    /// </summary>
    public partial class MessageQueue
    {
        //private static readonly MessageQueue Instance = new MessageQueue("");

        static ConnectionFactory factory;

        /// <summary>
        /// RabbitMQ 连接对象
        /// </summary>
        private IConnection Connection { get; set; }

        /// <summary>
        /// RabbitMQ 连接频道
        /// </summary>
        private IModel Channel { get; set; }

        private string ExchangeName { get; set; }

        /// <summary>
        /// 消息处理成功标志
        /// </summary>
        public bool IsOperationOk { get; set; } = false;

        ///// <summary>
        ///// 初始化<see cref="MessageQueue"/>
        ///// </summary>
        ///// <param name="data"></param>
        //private MessageQueue(string data)
        //{
        //    var hostname = ConfigurationManager.AppSettings["Rabbitmq.hostname"] ?? "localhost";
        //    var username = ConfigurationManager.AppSettings["Rabbitmq.username"] ?? "guest";
        //    var password = ConfigurationManager.AppSettings["Rabbitmq.password"] ?? "guest";
        //    var vhost = ConfigurationManager.AppSettings["Rabbitmq.vhost"] ?? "/";

        //    factory = new ConnectionFactory()
        //    {
        //        HostName = hostname,
        //        UserName = username,
        //        Password = password,
        //        VirtualHost = vhost
        //    };
        //    factory.Port = AmqpTcpEndpoint.UseDefaultPort;          //端口号
        //}

        /// <summary>
        /// 初始化加载RabbitMQ配置参数
        /// </summary>
        /// <param name="data"></param>
        private void InitRabbitMQConfig()
        {
            var hostname = ConfigurationManager.AppSettings["Rabbitmq.hostname"] ?? "localhost";
            var username = ConfigurationManager.AppSettings["Rabbitmq.username"] ?? "guest";
            var password = ConfigurationManager.AppSettings["Rabbitmq.password"] ?? "guest";
            var vhost = ConfigurationManager.AppSettings["Rabbitmq.vhost"] ?? "/";

            factory = new ConnectionFactory()
            {
                HostName = hostname,
                UserName = username,
                Password = password,
                VirtualHost = vhost
            };
            factory.Port = AmqpTcpEndpoint.UseDefaultPort;          //端口号
        }

        /// <summary>
        /// 实例化<see cref="MessageQueue"/>
        /// </summary>
        public MessageQueue()
        {
            InitRabbitMQConfig();
            ExchangeName = ConfigurationManager.AppSettings["Rabbitmq.exchangeName"] ?? "dykj_mq.direct";

            try
            {
                Connection = factory.CreateConnection();
                Channel = Connection.CreateModel();
            }
            catch (System.Exception ex)
            {
                //DySoft.Logging.Log.Error(ex, "RabbitMQ初始化异常！");
            }
        }

        /// <summary>
        /// 声明队列
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="temp">是否为临时队列</param>
        /// <remarks>临时队列会在连接结束后自动删除</remarks>
        public void QueueDeclare(string queueName, bool temp = true)
        {
            if (temp)
            {
                Channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: true);
            }
            else
            {
                Channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false);
            }
        }

        /// <summary>
        /// 绑定队列与广播
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="broadcastName">广播名称</param>
        public void BindQueue(string queueName, string broadcastName)
        {
            Channel.QueueBind(queueName, ExchangeName, broadcastName);
        }


    }
}
